#include "USBfnBus.h"

USBfnBus::USBfnBus(void):m_ufnHandle(INVALID_HANDLE_VALUE)
{

}

USBfnBus::~USBfnBus(void)
{
	Close();
}

DWORD USBfnBus::Open(void)
{
	m_ufnHandle  = CreateFile(L"UFN1:",0,0,NULL,OPEN_EXISTING,0,NULL);
    if(m_ufnHandle == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }
	return TRUE;
}
DWORD USBfnBus::Close(void)
{
    if(m_ufnHandle != INVALID_HANDLE_VALUE)
    {
        CloseHandle(m_ufnHandle);
		m_ufnHandle = INVALID_HANDLE_VALUE;
    }
	return TRUE;
}

DWORD USBfnBus::EnumClientsStart()
{
	return DeviceIoControl(m_ufnHandle,
		IOCTL_UFN_ENUMERATE_AVAILABLE_CLIENTS_SETUP,
		NULL,0,NULL,0,NULL,0);

}

DWORD USBfnBus::EnumClients(UFN_CLIENT_INFO * pCInfo)
{
	DWORD nRet;
	DWORD dwBytes;
	nRet=DeviceIoControl(m_ufnHandle,
		IOCTL_UFN_ENUMERATE_AVAILABLE_CLIENTS,
		pCInfo,sizeof(UFN_CLIENT_INFO),pCInfo,sizeof(UFN_CLIENT_INFO),&dwBytes,0);
	GetLastError();
	return nRet;
}

DWORD USBfnBus::GetCurrentClient(UFN_CLIENT_INFO * pCInfo)
{
	DWORD nRet;
	DWORD dwBytes=0;
	nRet=DeviceIoControl(m_ufnHandle,
		IOCTL_UFN_GET_CURRENT_CLIENT,
		NULL,0,pCInfo,sizeof(UFN_CLIENT_INFO),&dwBytes,0);
	GetLastError();
	return nRet;
}